package com.global.college.utils;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.global.college.R;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 类描述：图片操作
 */
public class BmpUtil {


    /**
     * 保存图片到系统相册目录
     *
     * @param bmp        位图对象
     * @param fileFolder 图片目录
     * @param filename   图片名称
     * @return 保存地址
     */
    public static String saveImageToSystemAlbum(Context context, Bitmap bmp, String fileFolder, String filename) {
        OutputStream stream = null;
        try {
            File saveDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), fileFolder);
            if (!saveDirectory.exists())
                saveDirectory.mkdirs();
            File saveFile = new File(saveDirectory, filename);
            if (!saveFile.exists())
                saveFile.createNewFile();
            stream = new BufferedOutputStream(new FileOutputStream(saveFile.getAbsolutePath()));
            Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
            int quality = 100;
            bmp.compress(format, quality, stream);

            // 发送系统广播更新相册
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(saveFile);
            intent.setData(uri);
            context.sendBroadcast(intent);
            return saveFile.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String saveImageToSystemAlbum(Context context, File file, String fileFolder, String filename) {
        OutputStream stream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            File saveDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), fileFolder);
            if (!saveDirectory.exists())
                saveDirectory.mkdirs();
            File saveFile = new File(saveDirectory, filename);
            if (!saveFile.exists())
                saveFile.createNewFile();

            bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            stream = new BufferedOutputStream(new FileOutputStream(saveFile.getAbsolutePath()));

            byte[] read = new byte[1024];
            while (bufferedInputStream.read(read) != -1) {
                stream.write(read);
            }
            stream.flush();

            // 发送系统广播更新相册
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(saveFile);
            intent.setData(uri);
            context.sendBroadcast(intent);
            return saveFile.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /*
     * 保存文件，文件名为当前日期
     */
    public static void saveBitmap(Context context, Bitmap bitmap, String bitName) {
        String fileName;
        File file;
        if (Build.BRAND.equals("Xiaomi")) { // 小米手机
            fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + bitName;
        } else {  // Meizu 、Oppo
            Log.v("qwe", "002");
            fileName = Environment.getExternalStorageDirectory().getPath() + "/DCIM/" + bitName;
            //fileName = HSApplication.getContext().getExternalFilesDir(Constants.KEY.DOWNLOAD_PATH).getAbsolutePath() + bitName;
        }
        file = new File(fileName);
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            // 格式为 JPEG，照相机拍出的图片为JPEG格式的，PNG格式的不能显示在相册中
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
                out.flush();
                out.close();
                // 插入图库
                //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), bitName, null);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
        // 发送广播，通知刷新图库的显示
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileName)));
        Toast.makeText(context, R.string.picture_had_alredy_sava, Toast.LENGTH_SHORT).show();

    }

    public static Bitmap getDiskBitmap(String path, String name) {
        Bitmap bitmap = null;
        try {
            File file = new File(path, name);
            //File file2 = new File(path,name);
            if (file.exists()) {
                bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

        return bitmap;
    }


    public static ContentValues getVideoContentValues(Context paramContext, File paramFile, long paramLong) {
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("title", paramFile.getName());
        localContentValues.put("_display_name", paramFile.getName());
        localContentValues.put("mime_type", "video/mp4");
        localContentValues.put("datetaken", Long.valueOf(paramLong));
        localContentValues.put("date_modified", Long.valueOf(paramLong));
        localContentValues.put("date_added", Long.valueOf(paramLong));
        localContentValues.put("_data", paramFile.getAbsolutePath());
        localContentValues.put("_size", Long.valueOf(paramFile.length()));
        return localContentValues;
    }
}

